package com.nation.controller;

import com.nation.dao.ContinentDao;
import com.nation.dao.DocumentDao;
import com.nation.domain.*;
import com.nation.service.*;
import com.nation.utils.CipherUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dqd on 2017/11/8.
 */
@Controller
@RequestMapping(value = "/api")
public class ApiController {
    @Autowired
    private ContinentService ContinentService;
    @Autowired
    private NationInfoService NationInfoService;
    @Autowired
    private NatureService NatureService;
    @Autowired
    private PoliticService PoliticService;
    @Autowired
    private DevelopmentService DevelopmentService;
    @Autowired
    private SocietyService SocietyService;
    @Autowired
    private TechnologyService TechnologyService;
    @Autowired
    private RelationshipService RelationshipService;
    @Autowired
    private OverseasService OverseasService;
    @Autowired
    private DocumentService DocumentService;
    @Autowired
    private ResearchService ResearchService;
    @Autowired
    private VUsersService VUsersService;

    @RequestMapping(value = "/find_all_continent")
    @ResponseBody
    public Result findAllContinent(){

        try{
            List<TContinentC> clist= ContinentService.findAllContinent();
            Result<List<TContinentC>> result=new Result<List<TContinentC>>();
            result.setStatus(200);
            result.setData(clist);
            return result;
        }catch(Exception e){
            Result<String> result=new Result<String>();
            result.setStatus(0);
            result.setData("服务器异常");
            return result;
        }
    }
    @RequestMapping(value = "/firstpage_find_nationinfo")
    @ResponseBody
    public Result find_nationinfo(Integer page,String continent_id){
        int start=page*10;
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("start",start);
        map.put("continent_id",continent_id);
        List<TNationInfo> tlist=NationInfoService.findNationInfoByLimit(map);
        Result<List<TNationInfo>> result=new Result<List<TNationInfo>>();
        if(tlist.size()==0){
            result.setStatus(201);//数据为空
        }else{
            result.setStatus(200);
            result.setData(tlist);
        }
        return result;
    }
    @RequestMapping(value = "/firstpage_find_nature")
    @ResponseBody
    public Result find_nature(Integer page,String continent_id){
        int start=page*10;
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("start",start);
        map.put("continent_id",continent_id);
        List<TNatureAndResource> tlist=NatureService.findNatureLimit(map);
        Result<List<TNatureAndResource>> result=new Result<List<TNatureAndResource>>();
        if(tlist.size()==0){
            result.setStatus(201);//数据为空
        }else{
            result.setStatus(200);
            result.setData(tlist);
        }
        return result;
    }
    @RequestMapping(value = "/firstpage_find_politic")
    @ResponseBody
    public Result find_politic(Integer page,String continent_id){
        int start=page*10;
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("start",start);
        map.put("continent_id",continent_id);
        List<VPoliticsAndMilitary> tlist=PoliticService.findPoliticByLimit(map);
        Result<List<VPoliticsAndMilitary>> result=new Result<List<VPoliticsAndMilitary>>();
        if(tlist.size()==0){
            result.setStatus(201);//数据为空
        }else{
            result.setStatus(200);
            result.setData(tlist);
        }
        return result;
    }
    @RequestMapping(value = "/firstpage_find_development")
    @ResponseBody
    public Result find_development(Integer page,String continent_id){
        int start=page*10;
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("start",start);
        map.put("continent_id",continent_id);
        List<VEconomicAndDevelopment> tlist=DevelopmentService.findByLimit(map);
        Result<List<VEconomicAndDevelopment>> result=new Result<List<VEconomicAndDevelopment>>();
        if(tlist.size()==0){
            result.setStatus(201);//数据为空
        }else{
            result.setStatus(200);
            result.setData(tlist);
        }
        return result;
    }
    @RequestMapping(value = "/firstpage_find_society")
    @ResponseBody
    public Result find_society(Integer page,String continent_id){
        int start=page*10;
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("start",start);
        map.put("continent_id",continent_id);
        List<VSocialAndCondition> tlist=SocietyService.findByLimit(map);
        Result<List<VSocialAndCondition>> result=new Result<List<VSocialAndCondition>>();
        if(tlist.size()==0){
            result.setStatus(201);//数据为空
        }else{
            result.setStatus(200);
            result.setData(tlist);
        }
        return result;
    }
    @RequestMapping(value = "/firstpage_find_technology")
    @ResponseBody
    public Result find_technology(Integer page,String continent_id){
        int start=page*10;
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("start",start);
        map.put("continent_id",continent_id);
        List<VScienceAndTechnologyEducation> tlist=TechnologyService.findByLimit(map);
        Result<List<VScienceAndTechnologyEducation>> result=new Result<List<VScienceAndTechnologyEducation>>();
        if(tlist.size()==0){
            result.setStatus(201);//数据为空
        }else{
            result.setStatus(200);
            result.setData(tlist);
        }
        return result;
    }
    @RequestMapping(value = "/firstpage_find_relationship")
    @ResponseBody
    public Result find_relationship(Integer page,String continent_id){
        int start=page*10;
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("start",start);
        map.put("continent_id",continent_id);
        List<VInternationalAndRelationship> tlist=RelationshipService.findByLimit(map);
        Result<List<VInternationalAndRelationship>> result=new Result<List<VInternationalAndRelationship>>();
        if(tlist.size()==0){
            result.setStatus(201);//数据为空
        }else{
            result.setStatus(200);
            result.setData(tlist);
        }
        return result;
    }
    @RequestMapping(value = "/firstpage_find_overseas")
    @ResponseBody
    public Result find_overseas(Integer page,String continent_id){
        int start=page*10;
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("start",start);
        map.put("continent_id",continent_id);
        List<VOverseasChinese> tlist=OverseasService.findByLimit(map);
        Result<List<VOverseasChinese>> result=new Result<List<VOverseasChinese>>();
        if(tlist.size()==0){
            result.setStatus(201);//数据为空
        }else{
            result.setStatus(200);
            result.setData(tlist);
        }
        return result;
    }

    @RequestMapping(value = "/find_nationinfo_pk")
    @ResponseBody
    public Result find_nationinfo_pk(String country_code){
        TNationInfo nationinfo=NationInfoService.findByCountryCode(country_code);
        Result<TNationInfo> result=new Result<TNationInfo>();
        if(nationinfo==null){
            result.setStatus(201);
        }else{
            result.setData(nationinfo);
            result.setStatus(200);
        }
        return result;
    }
    @RequestMapping(value = "/find_nature_pk")
    @ResponseBody
    public Result find_nature_pk(String country_code){
        TNatureAndResource nature=NatureService.findByCountryCode(country_code);
        Result<TNatureAndResource> result=new Result<TNatureAndResource>();
        if(nature==null){
            result.setStatus(201);
        }else{
            result.setData(nature);
            result.setStatus(200);
        }
        return result;
    }
    @RequestMapping(value = "/find_politic_pk")
    @ResponseBody
    public Result find_politic_pk(String country_code){
        VPoliticsAndMilitary politic=PoliticService.findByCountryCode(country_code);
        Result<VPoliticsAndMilitary> result=new Result<VPoliticsAndMilitary>();
        if(politic==null){
            result.setStatus(201);
        }else{
            result.setData(politic);
            result.setStatus(200);
        }
        return result;
    }
    @RequestMapping(value = "/find_development_pk")
    @ResponseBody
    public Result find_development_pk(String country_code){
        VEconomicAndDevelopment development=DevelopmentService.findByCountryCode(country_code);
        Result<VEconomicAndDevelopment> result=new Result<VEconomicAndDevelopment>();
        if(development==null){
            result.setStatus(201);
        }else{
            result.setData(development);
            result.setStatus(200);
        }
        return result;
    }
    @RequestMapping(value = "/find_society_pk")
    @ResponseBody
    public Result find_society_pk(String country_code){
        VSocialAndCondition society=SocietyService.findByCountryCode(country_code);
        Result<VSocialAndCondition> result=new Result<VSocialAndCondition>();
        if(society==null){
            result.setStatus(201);
        }else{
            result.setData(society);
            result.setStatus(200);
        }
        return result;
    }
    @RequestMapping(value = "/find_technology_pk")
    @ResponseBody
    public Result find_technology_pk(String country_code){
        VScienceAndTechnologyEducation tech=TechnologyService.findByCountryCode(country_code);
        Result<VScienceAndTechnologyEducation> result=new Result<VScienceAndTechnologyEducation>();
        if(tech==null){
            result.setStatus(201);
        }else{
            result.setData(tech);
            result.setStatus(200);
        }
        return result;
    }
    @RequestMapping(value = "/find_relationship_pk")
    @ResponseBody
    public Result find_relationship_pk(String country_code){
        VInternationalAndRelationship relationship=RelationshipService.findByCountryCode(country_code);
        Result<VInternationalAndRelationship> result=new Result<VInternationalAndRelationship>();
        if(relationship==null){
            result.setStatus(201);
        }else{
            result.setData(relationship);
            result.setStatus(200);
        }
        return result;
    }
    @RequestMapping(value = "/find_overseas_pk")
    @ResponseBody
    public Result find_overseas_pk(String country_code){
        VOverseasChinese overseas=OverseasService.findByCountryCode(country_code);
        Result<VOverseasChinese> result=new Result<VOverseasChinese>();
        if(overseas==null){
            result.setStatus(201);
        }else{
            result.setData(overseas);
            result.setStatus(200);
        }
        return result;
    }
    @RequestMapping(value = "/find_document")
    @ResponseBody
    public Result find_document(int page){
        int start=page*10;
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("start",start);
        //map.put("continent_id",continent_id);
        List<VImportantLiteratureData> tlist=DocumentService.findByLimit(map);
        Result<List<VImportantLiteratureData>> result=new Result<List<VImportantLiteratureData>>();
        if(tlist.size()==0){
            result.setStatus(201);//数据为空
        }else{
            result.setStatus(200);
            result.setData(tlist);
        }
        return result;
    }
    @RequestMapping(value = "/find_research")
    @ResponseBody
    public Result find_research(int page){
        int start=page*10;
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("start",start);
        //map.put("continent_id",continent_id);
        List<VImportantResearchWork> tlist= ResearchService.findByLimit(map);
        Result<List<VImportantResearchWork>> result=new Result<List<VImportantResearchWork>>();
        if(tlist.size()==0){
            result.setStatus(201);//数据为空
        }else{
            result.setStatus(200);
            result.setData(tlist);
        }
        return result;
    }
    @RequestMapping(value = "/find_document_pk")
    @ResponseBody
    public Result find_document_pk(String document_code){
        VImportantLiteratureData data=DocumentService.findByDocumentCode(document_code);
        Result<VImportantLiteratureData> result=new Result<VImportantLiteratureData>();
        result.setStatus(200);
        result.setData(data);
        return result;
    }
    @RequestMapping(value = "/find_research_pk")
    @ResponseBody
    public Result find_research_pk(int id){
        VImportantResearchWork data=ResearchService.findById(id);
        Result<VImportantResearchWork> result=new Result<VImportantResearchWork>();
        result.setStatus(200);
        result.setData(data);
        return result;
    }
    @RequestMapping(value = "/find_userinfo")
    @ResponseBody
    public Result find_userinfo(String account){
        VUsers vUsers=VUsersService.findUserByAccount(account);
        List<TCityC> clist=VUsersService.findAllCity();
        List<TProvinceC> plist=VUsersService.findAllProvince();
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("users",vUsers);
        map.put("cities",clist);
        map.put("provinces",plist);
        Result<Map<String,Object>> result=new Result<Map<String, Object>>();
        if(vUsers==null){
            result.setStatus(201);
        }else{
            result.setData(map);
            result.setStatus(200);
        }
        return result;
    }
    @RequestMapping(value = "/update_userinfo")
    @ResponseBody
    public Result update_userinfo(VUsers vUsers, MultipartFile file,HttpServletRequest request){
        String path=request.getSession().getServletContext().getRealPath("/userPicture");
        String name=fileSave(file,path,vUsers.getAccount());
        if(name==null){
            Result<String> result = new Result<String>();
            result.setData("服务器异常，头像上传失败");
            result.setStatus(0);
            return result;
        }

        vUsers.setPicture(path.substring(2,path.length())+"\\"+name);System.out.println(vUsers.getPicture()+"-path-");
        Boolean judge=VUsersService.updateUsersByAccount(vUsers);
        if(judge){
            VUsers user=VUsersService.findUserByAccount(vUsers.getAccount());
            List<TCityC> clist=VUsersService.findAllCity();
            List<TProvinceC> plist=VUsersService.findAllProvince();
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("users",user);
            map.put("cities",clist);
            map.put("provinces",plist);
            Result<Map<String,Object>> result=new Result<Map<String, Object>>();
            result.setStatus(200);
            result.setData(map);
            return result;
        }else{
            Result<String> result=new Result<String>();
            result.setStatus(0);
            result.setData("服务器异常，修改失败");
            return result;
        }
    }
    @RequestMapping(value="/user_login")
    @ResponseBody
    public Result user_login(String account,String password){
        VUsers users = VUsersService.findUserByAccount(account);
        if(users==null){
            Result<String> result=new Result<String>();
            result.setData("用户不存在请注册");
            result.setStatus(201);
            return result;
        }else{
            String getPass=CipherUtil.generatePassword(password,users.getSalt());
            if(!getPass.equals(password)){
                Result<String> result=new Result<String>();
                result.setData("用户或密码错误");
                result.setStatus(202);//用户名或密码错误
                return result;
            }else{
                Result<VUsers> result=new Result<VUsers>();
                result.setData(users);
                result.setStatus(200);
                return result;
            }
        }
    }
    private String fileSave(MultipartFile file,String path,String newName){
        String fileName=file.getOriginalFilename();
        int index=fileName.indexOf('.');
        newName = newName+fileName.substring(index,fileName.length());
        File newfile=new File(path,newName);
        if(!newfile.exists()){
            newfile.mkdir();
        }
        try {
            file.transferTo(newfile);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        return newName;
    }
}
